var tablename = "calendar";
var app = this;
var $ = require("../../include/$");
module.exports = {
	get: function(req, res) {
		console.log("查询表“", tablename, "”的数据", req.body, req.query);
		var data=req.query;
		var finner=req.models[tablename].find(data,[ "start", "Z" ]);;
		// if (req.query.sid) {
		// 	finner = req.models[tablename].find({
		// 		"sid": req.query.sid
		// 	});
		// } else {
		// 	finner = req.models[tablename].find({
		// 		"uid": req.query.uid
		// 	});
		// };
		finner.run(function(err, result) {
			if (err) {
				console.log(err)
				res.send(500, err);
			} else if (!result.length) {
				res.send(404, "no found");
			} else {
				var _RES = {};
				_RES.result = result;
				_RES.length = result.length;
				_RES.done = 0;
				for (var i = _RES.length - 1; i >= 0; i--) {
					if (result[i].color != "0") {
						_RES.done += 1;
					};
				};
				res.send(_RES);
				res.end();
			};
		})
	},
	post: function(req, res) {
		console.log(req.body);
		req.models.calendar.create($.extend({}, req.body), function(err, result) {
			if (err) {
				res.send(500, err);
			} else {
				res.send("ok");
				console.log("success");
				res.end();
			};
		})
	},
	put: function(req, res) {
		var id = req.body.id;
		console.log("为表“", tablename, "”进行修改：", req.body, req.query);
		req.models[tablename].get(id, function(err, result) {
			if (err) {
				res.send(500, err);
			} else {
				for (var color in result) {
					result.color = req.body.color;
				};
				result.save(function(err) {
					if (err) {
						res.send(500, err);
					} else {
						res.send("ok");
						res.end();
					}
				});
			}
		});
	},
	delete: function(req, res) {
		req.models[tablename].get(req.body.id, function(err, result) {
			if (err) {
				res.send(500, err);
			} else {
				for (var id in result) {
					result.id = req.body.id;
				};
				result.remove(function(err) {
					if (err) {
						res.send(500, err);
					} else {
						// res.send(result);
						//返回新的数据列表
						res.end();
					}
				});
			}
		});
	}
}